import { queryCategoryList } from "@/api/category.api";
import { Category, CategoryList } from "@/types/category";
import { useCallback, useEffect, useState } from "react";

interface CategoryData {
  list: CategoryList;
  current: Category | null;
}
export const useCategory = (): {
  categoryList: CategoryData;
  setCategoryList:(categoryData:CategoryData)=>void
} => {
  const [categoryList,setCategoryList ]= useState<CategoryData>({
    list: [],
    current: null,
  });
  const getCateGoryList =  useCallback( async () => {
    const res = await queryCategoryList();
    if (res.code === 200) {
      setCategoryList({
        current:categoryList.current,
        list:res.data||[]
      })
    }
  },[categoryList]) 
  useEffect(()=>{
    getCateGoryList();
  // eslint-disable-next-line react-hooks/exhaustive-deps
  },[])
  
  return {
    categoryList,
    setCategoryList
  };
};
